package View;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author elach_000
 */
public class GameView extends JFrame {

    private BoardView board;
    private MonitorView monitor;

    public GameView() {
        this.board = new BoardView();
        this.monitor = new MonitorView();
        initGameView();

    }

    public GameView(BoardMap boardMap) {
        this.board = new BoardView(boardMap);
        this.monitor = new MonitorView();
        initGameView();
    }

    public BoardView getBoard() {
        return board;
    }

    private void initGameView() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setSize(672,480); //To see map borders
        //setSize(608,430); //No map borders
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("Game View");
        setResizable(true);
        setVisible(true);

        this.addComponentListener(new ComponentListener() {
            public void componentResized(ComponentEvent e) {
                Dimension windowDim = ((GameView) e.getSource()).getBoard().getSize();
                board.getCamera().setWindowHeight(windowDim.height);
                board.getCamera().setWindowWidth(windowDim.width);
                board.getCamera().adaptCamera();
            }

            public void componentMoved(ComponentEvent e) {
            }

            public void componentShown(ComponentEvent e) {
            }

            public void componentHidden(ComponentEvent e) {
            }
        });

        this.setLayout(new BorderLayout());

        this.add(board, BorderLayout.CENTER);
        
        this.add(monitor,BorderLayout.EAST);

        JToolBar toolBar = new JToolBar();


        JCheckBox showXYCheckbox = new JCheckBox("Show Coordinates", board.getCamera().showXY);
        showXYCheckbox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                board.getCamera().showXY = !board.getCamera().showXY;
                board.repaint();
            }
        });

        JButton zoomPlusButton = new JButton("Zoom +");
        zoomPlusButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                board.getCamera().zoomPlus();
                board.repaint();
            }
        });
        JButton zoomMoinsButton = new JButton("Zoom -");

        zoomMoinsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                board.getCamera().zoomMoins();
                board.repaint();
            }
        });
        zoomMoinsButton.setFocusable(false);
        zoomPlusButton.setFocusable(false);
        showXYCheckbox.setFocusable(false);

        JLabel cameraSpeedLabel = new JLabel("Camera Speed");
        cameraSpeedLabel.setFocusable(false);

        JSlider cameraSpeedSlider = new JSlider(1, 20, board.getCamera().cameraSpeed);
        cameraSpeedSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent event) {
                int value = ((JSlider) event.getSource()).getValue();
                board.getCamera().cameraSpeed = value;
            }
        });
        cameraSpeedSlider.setFocusable(false);
        toolBar.add(cameraSpeedLabel);
        toolBar.add(cameraSpeedSlider);

        toolBar.add(zoomPlusButton);
        toolBar.add(zoomMoinsButton);
        toolBar.add(showXYCheckbox);

        this.add(toolBar, BorderLayout.SOUTH);
        //pack();
    }

    public MonitorView getMonitor() {
        return monitor;
    }

    public void setMonitor(MonitorView monitor) {
        this.monitor = monitor;
    }
}
